<?php

namespace App\Http\Controllers\Back;

use App\Util\Result;
use App\Model\Role;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class IndexController extends Controller
{
    public function __construct(Request $request){
        $this->request = $request;
    }

    /**
     * 后台主要控制台
     * @author gaoyoulong
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function console()
    {
        return view('back.console')->with('userInfo',session('admin_info'));
    }

    public function index(){
        return view('back.index.applist');
    }

    public function account(){
        $role = Role::getOne($this->userInfo->role_id)->getData();
        return view('admin.index.account')->with('roleName',$role->title);
    }

    public function editpwd(Request $request){
        if($request->isMethod('POST')){
            $postData = $request->all();
            $msg = "";
            if (!isset($postData["old_pwd"]) || $postData["old_pwd"]==='') $msg .= "旧密码,";
            if (!isset($postData["new_pwd"]) || $postData["new_pwd"]==='') $msg .= "新密码,";
            if (!isset($postData["confirm"]) || $postData["confirm"]==='') $msg .= "确认密码,";
            if ($msg) {
            	$msg = substr($msg, 0, -1)."不能为空，请重试！";
            	$return = array(
            			'success' => false,
            			'msg' => $msg
            	);
            	return $return;
            }
            
            if(!password_verify($postData['old_pwd'], $this->userInfo->password))
            {
                return response()->json(Result::failure('旧密码不正确')->getResult());
            }
            else if($postData['new_pwd'] !== $postData['confirm']){
                return response()->json(Result::failure('两次输入密码不相同')->getResult());
            }
            else{
                $this->userInfo->password = bcrypt($postData['new_pwd']);
                $this->userInfo->save();

                return response()->json(Result::success('修改成功')->getResult());
            }
        }
        else{
            return view('auth.reset');
        }
    }
}
